/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package tests.flexunit.comparers
{
	import flexunit.framework.AssertionFailedError;
	import flexunit.framework.TestCase;
	
	import funit.resources.AssertResources;
	
	import sv.data.comparers.NumericComparer;
	import sv.utils.ClassUtil;
	
	public class NumericComparerTests extends TestCase
	{
		private var numericComparer:NumericComparer;
		
		
		public function NumericComparerTests( methodName:String = null )
		{
			super(methodName);
		}
		
		
		override public function setUp() : void
		{
			numericComparer = new NumericComparer();
		}
		
		///////////////////////////////////////////
		// Compare Tests
		///////////////////////////////////////////
		
		public function testCompareHandleNulls() : void
		{
			var number1:Number = 123.456;
			
			assertEquals( 0, numericComparer.compare(null, null) );
			
			assertEquals(-1, numericComparer.compare(null, number1) );
			assertEquals( 1, numericComparer.compare(number1, null) );
		}
		
		public function testCompareHandleNaN() : void
		{
			var number1:Number = 123.456;
			
			assertEquals( 0, numericComparer.compare(NaN, NaN) );
			
			assertEquals(-1, numericComparer.compare(NaN, number1) );
			assertEquals( 1, numericComparer.compare(number1, NaN) );
		}
		
		public function testCompareHandleNullAndNaN() : void
		{
			assertEquals( 0, numericComparer.compare(NaN, NaN) );
			
			assertEquals(-1, numericComparer.compare(null, NaN) );
			assertEquals( 1, numericComparer.compare(NaN, null) );
		}
		
		public function testCompareHandleNonFinite() : void
		{
			assertEquals( 0, numericComparer.compare( Infinity,  Infinity) );
			assertEquals( 0, numericComparer.compare(-Infinity, -Infinity) );
			
			assertEquals(-1, numericComparer.compare(-Infinity,  Infinity) );
			assertEquals( 1, numericComparer.compare( Infinity, -Infinity) );
			
			assertEquals(-1, numericComparer.compare( null,  Infinity) );
			assertEquals(-1, numericComparer.compare( null, -Infinity) );
			assertEquals( 1, numericComparer.compare( Infinity, null) );
			assertEquals( 1, numericComparer.compare(-Infinity, null) );
			
			assertEquals(-1, numericComparer.compare( NaN,  Infinity) );
			assertEquals(-1, numericComparer.compare( NaN, -Infinity) );
			assertEquals( 1, numericComparer.compare( Infinity, NaN) );
			assertEquals( 1, numericComparer.compare(-Infinity, NaN) );
		}
		
		public function testCompareHandleAreSame() : void
		{
			var number1:Number = 123.456;
			
			assertEquals( 0, numericComparer.compare(number1, number1) );
		}
		
		public function testCompareNumber() : void
		{
			var number1:Number = 123.456;
			var number2:Number = 123.456;
			var number3:Number = 654.321;
			
			assertEquals( 0, numericComparer.compare(number1, number2) );
			
			assertEquals(-1, numericComparer.compare(number2, number3) );
			assertEquals( 1, numericComparer.compare(number3, number2) );
		}
		
		public function testCompareInt() : void
		{
			var number1:int = 123456;
			var number2:int = 123456;
			var number3:int = 654321;
			
			assertEquals( 0, numericComparer.compare(number1, number2) );
			
			assertEquals(-1, numericComparer.compare(number2, number3) );
			assertEquals( 1, numericComparer.compare(number3, number2) );
		}
		
		public function testCompareFailureOnTypeMismatch() : void
		{
			var number1:Number = 123.456;
			var string:String = "123.456";
			
			try
			{
				numericComparer.compare(number1, string);
			}
			catch ( e:ArgumentError )
			{
				return;
			}
			catch ( e:Error )
			{
				throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.UnexpectedErrorType", ClassUtil.getQualifiedClassName(e))
						);
			}
			
			throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.ErrorTypeExpected", ClassUtil.getQualifiedClassName(ArgumentError))
						);
		}
		
		public function testCompareFailureOnTypeMismatch2() : void
		{
			var number1:Number = 123.456;
			var string:String = "123.456";
			
			try
			{
				numericComparer.compare(string, number1);
			}
			catch ( e:ArgumentError )
			{
				return;
			}
			catch ( e:Error )
			{
				throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.UnexpectedErrorType", ClassUtil.getQualifiedClassName(e))
						);
			}
			
			throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.ErrorTypeExpected", ClassUtil.getQualifiedClassName(ArgumentError))
						);
		}
		
		///////////////////////////////////////////
		// Equals Tests
		///////////////////////////////////////////
		
		public function testEqualsHandleNulls() : void
		{
			var number1:Number = 123.456;
			
			assertTrue( numericComparer.equals(null, null) );
			
			assertFalse( numericComparer.equals(null, number1) );
			assertFalse( numericComparer.equals(number1, null) );
		}
		
		public function testEqualsHandleNaN() : void
		{
			var number1:Number = 123.456;
			
			assertTrue( numericComparer.equals(NaN, NaN) );
			
			assertFalse( numericComparer.equals(NaN, number1) );
			assertFalse( numericComparer.equals(number1, NaN) );
		}
		
		public function testEqualsHandleNullAndNaN() : void
		{
			assertFalse( numericComparer.equals(null, NaN) );
			assertFalse( numericComparer.equals(NaN, null) );
		}
		
		public function testEqualsHandleNonFinite() : void
		{
			assertTrue( numericComparer.equals( Infinity,  Infinity) );
			assertTrue( numericComparer.equals(-Infinity, -Infinity) );
			
			assertFalse( numericComparer.equals( Infinity, -Infinity) );
			assertFalse( numericComparer.equals( null, Infinity) );
			assertFalse( numericComparer.equals( NaN, Infinity) );
		}
		
		public function testEqualsHandleAreSame() : void
		{
			var number1:Number = 123.456;
			
			assertTrue( numericComparer.equals(number1, number1) );
		}
		
		public function testEqualsNumber() : void
		{
			var number1:Number = 123.456;
			var number2:Number = 123.456;
			var number3:Number = 654.321;
			
			assertTrue( numericComparer.equals(number1, number2) );
			
			assertFalse( numericComparer.equals(number2, number3) );
			assertFalse( numericComparer.equals(number3, number2) );
		}
		
		public function testEqualsInt() : void
		{
			var number1:int = 123456;
			var number2:int = 123456;
			var number3:int = 654321;
			
			assertTrue( numericComparer.equals(number1, number2) );
			
			assertFalse( numericComparer.equals(number2, number3) );
			assertFalse( numericComparer.equals(number3, number2) );
		}
		
		public function testEqualsFailureOnTypeMismatch() : void
		{
			var number1:Number = 123.456;
			var string:String = "123.456";
			
			try
			{
				numericComparer.equals(number1, string);
			}
			catch ( e:ArgumentError )
			{
				return;
			}
			catch ( e:Error )
			{
				throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.UnexpectedErrorType", ClassUtil.getQualifiedClassName(e))
						);
			}
			
			throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.ErrorTypeExpected", ClassUtil.getQualifiedClassName(ArgumentError))
						);
		}
		
		public function testEqualsFailureOnTypeMismatch2() : void
		{
			var number1:Number = 123.456;
			var string:String = "123.456";
			
			try
			{
				numericComparer.equals(string, number1);
			}
			catch ( e:ArgumentError )
			{
				return;
			}
			catch ( e:Error )
			{
				throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.UnexpectedErrorType", ClassUtil.getQualifiedClassName(e))
						);
			}
			
			throw	new AssertionFailedError(
							AssertResources.getString("Errors.AssertFailedError.ErrorTypeExpected", ClassUtil.getQualifiedClassName(ArgumentError))
						);
		}
		
	}
	
}